/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrn.Servlets;

import br.edu.ifrn.Classes.Curso;
import br.edu.ifrn.Classes.Disciplina;
import br.edu.ifrn.Dao.CursoDao;
import br.edu.ifrn.Dao.DisciplinaDao;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author MONTENEGRO
 */
public class GerenciarDisciplina extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String acao = request.getParameter("acao");
        
        if (acao.equals("InserirDisciplina")) {
            String nomeDisciplina = request.getParameter("nomeDisciplina");
            String cargaHoraria = request.getParameter("cargaHoraria");
            int idCurso = Integer.parseInt(request.getParameter("selectDisciplina"));
            
            Disciplina disciplina = new Disciplina();
            disciplina.setNomeDisciplina(nomeDisciplina);
            disciplina.setCargaHoraria(cargaHoraria);
            disciplina.setIdCurso(idCurso);
            
            DisciplinaDao disciplinaDao = new DisciplinaDao();
            disciplinaDao.InserirDisciplina(disciplina);
            
            HttpSession session = request.getSession();
            List<Disciplina> disciplinas = disciplinaDao.getLista();
            session.setAttribute("disciplinas", disciplinas);
            
            RequestDispatcher rd = request.getRequestDispatcher("/Adm/PaginaDisciplina.jsp");
            rd.forward(request, response);
            
        } else if (acao.equals("RemoverDisciplina")) {
            int idDisciplina = Integer.parseInt(request.getParameter("idDisciplina"));
            
            Disciplina disciplina = new Disciplina();
            disciplina.setIdDisciplina(idDisciplina);
            
            DisciplinaDao disciplinaDao = new DisciplinaDao();
            disciplinaDao.RemoverDisciplina(disciplina);
            
            HttpSession session = request.getSession();
            List<Disciplina> disciplinas = disciplinaDao.getLista();
            session.setAttribute("disciplinas", disciplinas);
            
            RequestDispatcher rd = request.getRequestDispatcher("/Adm/PaginaDisciplina.jsp");
            rd.forward(request, response);
            
        } else if (acao.equals("EditarDisciplina")) {
            int idDisciplina = Integer.parseInt(request.getParameter("idDisciplina"));
            String nomeDisciplina = request.getParameter("nomeDisciplina");
            String cargaHoraria = request.getParameter("cargaHoraria");
            int idCurso = Integer.parseInt(request.getParameter("selectDisciplina"));
            
            Disciplina disciplina = new Disciplina();
            disciplina.setIdDisciplina(idDisciplina);
            disciplina.setNomeDisciplina(nomeDisciplina);
            disciplina.setCargaHoraria(cargaHoraria);
            disciplina.setIdCurso(idCurso);
            
            DisciplinaDao disciplinaDao = new DisciplinaDao();
            disciplinaDao.EditarDisciplina(disciplina);
            
            HttpSession session = request.getSession();
            List<Disciplina> disciplinas = disciplinaDao.getLista();
            session.setAttribute("disciplinas", disciplinas);
            
            RequestDispatcher rd = request.getRequestDispatcher("/Adm/PaginaDisciplina.jsp");
            rd.forward(request, response);
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String acao = request.getParameter("acao");
        
        if (acao == null) {
            acao = "kkkk";
            
        }
        
        if (acao.equals("RemoverDisciplina")) {
            int idDisciplina = Integer.parseInt(request.getParameter("idDisciplina"));
            
            Disciplina disciplina = new Disciplina();
            disciplina.setIdDisciplina(idDisciplina);
            
            DisciplinaDao disciplinaDao = new DisciplinaDao();
            disciplinaDao.RemoverDisciplina(disciplina);
            
            HttpSession session = request.getSession();
            List<Disciplina> disciplinas = disciplinaDao.getLista();
            session.setAttribute("disciplinas", disciplinas);
            
            RequestDispatcher rd = request.getRequestDispatcher("/Adm/PaginaDisciplina.jsp");
            rd.forward(request, response);
            
        } else {
            
            HttpSession session = request.getSession();
            DisciplinaDao disciplinaDao = new DisciplinaDao();
            CursoDao cursoDao = new CursoDao();
            
            List<Disciplina> disciplinas = disciplinaDao.getLista();
            List<Curso> cursos = cursoDao.getLista();
            
            session.setAttribute("disciplinas", disciplinas);
            session.setAttribute("cursos", cursos);
            
            RequestDispatcher rd = request.getRequestDispatcher("/Adm/PaginaDisciplina.jsp");
            rd.forward(request, response);
       
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
